//
//  UIViewController+BarItem.m
//  KKZ
//
//  Created by  on 11-7-23.
//  Copyright 2011年 Tsinghua University. All rights reserved.
//

#import "UIViewController+BarItem.h"

@implementation UIViewController (BarItem)

- (void)createBarItemOn:(NSString *)side frame:(CGRect)frame title:(NSString *)title target:(id)target action:(SEL)anAction
{
    UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [aButton setFrame:frame];
    [aButton setTitle:title forState:UIControlStateNormal];
    [aButton.titleLabel setFont:[UIFont systemFontOfSize:14.0f]];
    [aButton setBackgroundImage:[UIImage imageNamed:@"navItem.png"] forState:UIControlStateNormal];
    [aButton addTarget:target action:anAction forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *aBarItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];
    if ([side isEqualToString:kNavBarItemSideLeft]) {
        self.navigationItem.leftBarButtonItem = aBarItem;
    }
    else {
        self.navigationItem.rightBarButtonItem = aBarItem;
    }
    
    [aBarItem release];
}

- (void)addTitleToNavigationBar:(NSString *)title
{
    UILabel *aLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 120, 44)];
    [aLabel setText:title];
    [aLabel setFont:[UIFont boldSystemFontOfSize:20.0f]];
    [aLabel setTextColor:[UIColor darkGrayColor]];
    [aLabel setTextAlignment:UITextAlignmentCenter];
    [aLabel setBackgroundColor:[UIColor clearColor]];
    [aLabel setAlpha:0.8];
    
    self.navigationItem.titleView = aLabel;
    //[aLabel release];
}

- (void)popBackToRootViewController
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)popBackToLastViewController
{
    [self.navigationController popViewControllerAnimated:YES];
}

@end
